
#include <iostream>
#include <Ogre/Ogre.h>
#include "MooseFlag.h"

using namespace Ogre;

class MooseCamera 
{
private:
	Camera *camera;				// pointer to a Ogre::Camera object
	SceneNode *target;			// pointer to target node.
	SceneNode *arrownode;
	Vector3 direction;			// direction to push camera back.
	MooseFlag *flags;
	
public:
	// constructor - setup a camera to use --------------------------
	MooseCamera::MooseCamera(SceneManager *sceneMgr, RenderWindow *window, SceneNode *target, MooseFlag *flags, bool isP1)
	{
		this->target = target;
		this->flags = flags;
		
		// Create a Camera
		camera = sceneMgr->createCamera("MooseCamera" + isP1);
		//Camera *cameraP2 = sceneMgr->createCamera("MooseCameraP2");

		// Create the viewport
		Viewport *view;
		if (isP1)
			view = window->addViewport(camera, 0, 0, 0, 1, 0.5);
		else
			view = window->addViewport(camera, 1, 0, 0.5, 1, 0.5);
		
		camera->setAspectRatio(Real(view->getActualWidth()/view->getActualHeight()));
		
		camera->setNearClipDistance(5);
		//cameraP2->setNearClipDistance(5);
		
//		camera->setFarClipDistance(1000);
		//update initial
		
		Ogre::Entity* arrowEntity = sceneMgr->createEntity( "arrowEntity" + isP1, "Line01.mesh" );
		arrowEntity->setCastShadows(false);
		
		if (isP1)
			arrowEntity->setMaterialName("ArrowRed");
		else
			arrowEntity->setMaterialName("ArrowBlue");
		
		arrownode = sceneMgr->getRootSceneNode()->createChildSceneNode( "arrownode" + isP1);
		arrownode->attachObject(arrowEntity);
		arrownode->setPosition(camera->getPosition() + camera->getOrientation()*Vector3(1000,600,2000));
		arrownode->scale(Vector3(0.5, 0.5, 0.5));
		
		update(target->getPosition());
	}
	
	void MooseCamera::update(Vector3 direction)
	{
		camera->setPosition(target->getPosition());
		
		camera->move( -10*direction);
		direction.normalise();
		camera->move( -100*direction);
		camera->move( 60*Vector3::UNIT_Y );
		
		// set its position, direction
		camera->lookAt(target->getPosition() + 40*Vector3::UNIT_Y);
//		camera->lookAt(arrownode->getPosition());
		arrownode->setPosition(camera->getPosition() + camera->getOrientation()*Vector3(0, 60,-200));
		arrownode->lookAt(Ogre::Vector3(flags->getPosition().x, arrownode->getPosition().y, flags->getPosition().z), Ogre::Node::TS_PARENT);
	}
};